package pruebas;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import junit.framework.TestCase;
import negModel.Combustible;

public class CombustibleTest extends TestCase {

	Combustible comb,comb2;
	Element e;
	
	protected void setUp() throws Exception {
		super.setUp();
		comb = new Combustible(88,1);

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();
		
		e = doc.createElement("combustible");
		
		e.setAttribute("cantidad", ""+comb.getCantidad());
		e.setAttribute("pesoxL", ""+comb.getPesoxL());
		e.setAttribute("octanos", ""+comb.getOctanos());
		
	}

	public void testGetPesoxL() {
		assertEquals(1,comb.getPesoxL(),0.1);
	}

	public void testGetOctanos() {
		assertEquals(88,comb.getOctanos(),0.1);
	}

	public void testSetCantidad() {
		comb.setCantidad(100);
		assertEquals(100,comb.getCantidad(),0.1);
	}

	public void testPesar() {
		comb.setCantidad(100);
		assertEquals(100,comb.pesar(),0.1);
	}

	public void testGastar() {
		assertEquals(0.01136,comb.gastar(),0.1);
	}
	
	public void testToXml () throws Exception {
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	    DocumentBuilder loader = factory.newDocumentBuilder();
	    Document doc = loader.newDocument();
		
		e=comb.toXml(doc);
		
		assertEquals(comb.getCantidad(),Double.parseDouble(e.getAttribute("cantidad")),0.0);
		assertEquals(comb.getPesoxL(),Double.parseDouble(e.getAttribute("pesoxL")),0.0);
		assertEquals(comb.getOctanos(),Float.parseFloat(e.getAttribute("octanos")),0.0);
		
	}
	
	public void testCombustible()throws Exception {
		
		comb2=new Combustible(e);
				
		assertEquals(comb2.getCantidad(),Double.parseDouble(e.getAttribute("cantidad")),0.0);
		assertEquals(comb2.getPesoxL(),Double.parseDouble(e.getAttribute("pesoxL")),0.0);
		assertEquals(comb2.getOctanos(),Float.parseFloat(e.getAttribute("octanos")),0.0);
	
	}

}
